package com.liudg.evan.web;


import com.evan.framework.core.base.BaseResult;
import com.evan.idempotent.aspect.Idempotent;
import com.evan.idempotent.aspect.IdempotentHandler;
import com.evan.idempotent.distributedlock.DistributedLock;
import com.evan.idempotent.exception.IdempotentException;
import com.evan.idempotent.idempotent.DistributedIdempotent;
import com.evan.idempotent.request.IdempotentRequest;
import com.liudg.evan.jacksondemo.service.impl.MessageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author liudeguang
 */
@RestController
public class IdempotentController {


    @Autowired
    private DistributedLock distributedLock;

    @Autowired
    private DistributedIdempotent distributedIdempotent;


    @GetMapping("/test")
    public String getUserName() {
        return "test succuessful";
    }

    @RequestMapping(value = "testLock", method = RequestMethod.GET)
    public String testLock(@RequestParam("key") String key) {
        //锁的释放时间一定要考虑好，不然业务处理时间太长了，导致锁被释放了，然后又调用了unLock()方法，就会出现错误
        distributedLock.lock(key, 300, (k, ex) -> {
            System.out.println("成功：" + k);
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }, (k, ex) -> {
            System.out.println("失败" + k);
        });
        return "succuessfull";
    }

    @RequestMapping(value = "testIdempotent", method = RequestMethod.GET)
    public String testIdempotent() {
        IdempotentRequest idempotentRequest = IdempotentRequest.builder()
                .key("123456")
                .storgeExpireTime(2)
                .timeUnit(TimeUnit.MINUTES)
                .build();

        Boolean isIdempotent = distributedIdempotent.execute("12345678", 2, TimeUnit.MINUTES);

        String isFlag = distributedIdempotent.execute(idempotentRequest, (k, ex) -> "success sout", (k, ex) -> "fail sout");

        return "succuessfull" + String.valueOf(isIdempotent);
    }


    /**
     * 这个key应该用一个分布式id生成器来生成，不能由用户自己提供
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "testIdempotentAnnotation", method = RequestMethod.GET)
//    @Idempotent(lockName = "test", spelKey = "#key", idempotentHandler = "idempotentHandler")
//    @Idempotent(lockName = "test",storgeExpireTime=10000)
//    @Idempotent(lockName = "test", idempotentHandler = "idempotentHandler")
    @Idempotent(lockName = "test", idempotentHandlerClass = {MessageServiceImpl.class})
//    public BaseResult testIdempotentAnnotation(@RequestParam("key") String key) {
    public BaseResult testIdempotentAnnotation() {
        System.out.println("我已经进来了");
        return BaseResult.defaultSuccess();
    }

    /**
     * 固定参数格式
     *
     * @param request 请求
     * @param e       异常，需要判断是否为null
     */
//    @IdempotentHandler
    public BaseResult idempotentHandler(IdempotentRequest request, IdempotentException e) {
        System.out.print(request.getKey() + ": idempotentHandler已经执行过了,");
        if (e != null) {
            System.out.println(e.toString());
        } else {
            System.out.println();
        }
        return BaseResult.defaultSuccess();
    }


}
